<script setup lang="ts">
import { ArrowDown, Delete, InfoFilled, Loading, Search,Upload as UploadIcon } from '@element-plus/icons-vue'
import OperatorPackageInfo from '@/views/main/platform-resource/operator-package/OperatorPackageInfo.vue'
import { onMounted, ref } from 'vue'
import {
  doDeleteOperatorPackage,
  doQueryOperatorPackageList,
  UploadOperatorPackageResponse,
  uploadOperatorPackageUrl
} from '@/api/operator-package-api'
import { accessToken } from '@/api/axios'
import type { UploadProgressEvent } from 'element-plus/es/components/upload/src/upload'
import type { BiuJsonResult } from '@/api/biu-json-result'
import { ElMessage } from 'element-plus'
import UploadResult from '@/views/main/platform-resource/operator-package/UploadResult.vue'

const data = ref([])

onMounted(() => {
  queryFunctionPackageList()
})

const keyword = ref()

function queryFunctionPackageList() {
  doQueryOperatorPackageList(keyword.value).then((res) => {
    data.value = res.data
  })
}

const showFunctionPackageInfo = ref(false)

const upload = ref({
  ing: false,
  percentage: 0,
  showDialog: false,
  result: {}
})

function uploadProgress(event: UploadProgressEvent) {
  upload.value.ing = true
  upload.value.percentage = event.percent
}

function uploadSuccess(res: BiuJsonResult<UploadOperatorPackageResponse>) {
  if (res.code !== 0) {
    upload.value.ing = false
    ElMessage({
      type: 'error',
      message: res.msg
    })
    return
  }
  upload.value.ing = false
  upload.value.showDialog = true
  upload.value.result = res.data
  queryFunctionPackageList()
}

function uploadError(error: Error) {
  upload.value.ing = false
  ElMessage({
    type: 'error',
    message: error.message
  })
}

const packageInfo = ref({
  id: '',
  name: '',
  tag: ''
})

const functionPackageListTableRef = ref()

function deleteFunctionPackage(id?: string) {
  if (id) {
    doDeleteOperatorPackage([id]).then(() => {
      ElMessage({
        type: 'success',
        message: '删除成功'
      })
      queryFunctionPackageList()
    })
  } else {
    doDeleteOperatorPackage(
      functionPackageListTableRef.value.getSelectionRows().map((row) => row.id)
    ).then(() => {
      ElMessage({
        type: 'success',
        message: '批量删除成功'
      })
      queryFunctionPackageList()
    })
  }
}
</script>

<template>
  <el-card class="function-package">
    <template #header>
      <div class="list-header">
        <div class="left-bar">
          <el-dropdown>
            <el-button type="primary">
              批量操作
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="deleteFunctionPackage()" :icon="Delete as any"
                  >删除
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        <div class="right-bar">
          <el-input v-model="keyword" size="default" placeholder="请输入关键字">
            <template #append>
              <el-button @click="queryFunctionPackageList" size="default" :icon="Search as any" />
            </template>
          </el-input>
          <el-upload
            :disabled="upload.ing"
            :action="uploadOperatorPackageUrl"
            :show-file-list="false"
            :on-progress="uploadProgress"
            :on-success="uploadSuccess"
            :on-error="uploadError"
            :headers="{
              'Access-Token': accessToken
            }"
          >
            <el-button v-if="!upload.ing" size="default" type="primary">
              上传
              <el-icon class="el-icon--right">
                <UploadIcon />
              </el-icon>
            </el-button>
            <el-button v-else size="default" type="primary">
              上传中&nbsp;{{ upload.percentage }}%
              <el-icon class="el-icon--right">
                <loading />
              </el-icon>
            </el-button>
          </el-upload>
        </div>
      </div>
    </template>
    <el-table
      ref="functionPackageListTableRef"
      max-height="calc(100vh - 240px)"
      :data="data"
      style="width: 100%"
    >
      <el-table-column type="selection" width="40" />
      <el-table-column prop="name" label="名称" width="180" />
      <el-table-column prop="operatorNumber" width="120" label="算子数量" />
      <el-table-column prop="tag" label="标签" />
      <el-table-column prop="createTime" sortable label="上传时间" />
      <el-table-column fixed="right" label="操作" width="160">
        <template #default="scope">
          <el-tooltip effect="dark" content="详情" placement="top">
            <el-button
              @click="
                () => {
                  showFunctionPackageInfo = true
                  packageInfo.id = scope.row.id
                  packageInfo.name = scope.row.name
                  packageInfo.tag = scope.row.tag
                }
              "
              type="primary"
              size="small"
              :icon="InfoFilled as any"
              circle
            />
          </el-tooltip>
          <el-tooltip effect="dark" content="删除" placement="top">
            <el-button
              @click="deleteFunctionPackage(scope.row.id)"
              type="danger"
              size="small"
              :icon="Delete as any"
              circle
            />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <operator-package-info
    :id="packageInfo.id"
    :name="packageInfo.name"
    :tag="packageInfo.tag"
    v-model="showFunctionPackageInfo"
  />
  <upload-result :result="upload.result" v-model="upload.showDialog" />
</template>

<style scoped lang="scss">
.function-package {
  .list-header {
    display: flex;
    align-items: center;
    justify-content: space-between;

    .right-bar {
      display: flex;
      align-items: center;

      > div {
        margin: 0 5px;
        display: flex;
        align-items: center;
      }
    }
  }
}
</style>
